package com.caogen.cg_wl_kt.base

import android.content.Context
import androidx.recyclerview.widget.RecyclerView

/**
 * 项目名称: cg_wl_kt
 * Packages:com.caogen.cg_wl_kt.base
 * author: FSW
 * 创建日期: 2024/2/29 09:16
 * Describe:
 *
 */
abstract class Base2Adapter<T, VH : RecyclerView.ViewHolder>(var mContext: Context) : RecyclerView.Adapter<VH>() {

    //ItemClick事件
    var mItemClickListener: OnItemClickListener<T>? = null

    var mItemLongClickListener: OnItemLongClickListener<T>? = null

    //数据集合
    var dataList: MutableList<T> = mutableListOf()

    /*
        设置数据
        Presenter处理过为null的情况，所以为不会为Null
     */
    fun setData(sources: MutableList<T>) {
        dataList = sources
        notifyDataSetChanged()
    }

    fun addAll(sources: MutableList<T>) {
        dataList.addAll(sources)
        notifyDataSetChanged()
    }

    fun clear() {
        dataList.clear()
        notifyDataSetChanged()
    }
    fun notifyDataSetChang() {
        notifyDataSetChanged()
    }

    override fun onBindViewHolder(holder: VH, position: Int) {

        holder.itemView.setOnClickListener {
            if (mItemClickListener != null)
                mItemClickListener!!.onItemClick(dataList[position], position)
        }

    }

    override fun getItemCount(): Int {
        return dataList.size
    }
    /*
        ItemClick事件声明
     */
    interface OnItemClickListener<in T> {
        fun onItemClick(item: T, position: Int)
    }

    interface OnItemLongClickListener<in T> {
        fun onItemLongClick(item: T, position: Int)
    }

    fun setOnItemClickListener(listener: OnItemClickListener<T>) {
        this.mItemClickListener = listener
    }

    fun setOnItemLongClickListener(listener: OnItemLongClickListener<T>) {
        this.mItemLongClickListener = listener
    }

}
